package com.ruiyangwang.ListenBook.servlet.album;

import com.ruiyangwang.ListenBook.repository.AlbumRepo;
import com.ruiyangwang.ListenBook.util.Log;
import com.ruiyangwang.ListenBook.viewObject.UserV0;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author syzygy
 * @date 2022/07/12 19:56
 **/
@WebServlet("/studio/albums/createAlbum.do")
public class CreateDOServlet extends HttpServlet {
    public final AlbumRepo albumRepo = new AlbumRepo();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        req.setCharacterEncoding("utf-8");
        UserV0 currentUser = null;
        if(session != null){
            currentUser = (UserV0) session.getAttribute("currentUser");
        }

        if(currentUser == null){
            throw new RuntimeException("用户未登录");
        }
        req.setCharacterEncoding("utf-8");
        String title = req.getParameter("title");
        String cover = req.getParameter("cover");
        Log.println("获取到的title为：" + title + "\n获取到的cover" + cover);
        int state = 1;
        albumRepo.insertAlbums(currentUser,title,cover,state);
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("/text/plain");
        PrintWriter writer = resp.getWriter();
        writer.println("专辑创建成功");
    }
}